#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
struct birth
{
	string name;
	int y;
	int m;
	int d;
	int k;
}s[N];
bool cmp(birth s1,birth s2)
{
	if(s1.y != s2.y )
	return s1.y < s2.y;
	else if (s1.m != s2.m )
	return s1.m < s2.m;
	else if(s1.d != s2.d )
	return s1.d < s2.d;
	else
	return s1.k > s2.k;
}
int main()
{
    int n;
	cin >> n;
	for(int i = 0;i<n;i++)	
	{
		cin >> s[i].name >> s[i].y >> s[i].m >> s[i].d;
		s[i].k = i;
	}
	sort(s,s+n,cmp);
	for(int i= 0;i<n;i++)
	{
		cout << s[i].name << endl;
	}
	
	return 0;
}
